﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.CApAPPTask
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using EngineeringInternalExtension;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Tecnomatix.Engineering;


namespace DnProcessSimulateCommands;

public abstract class CApAPPTask
{
  private KiAppTaskEx m_kiTask;
  private ITxOperation m_operation;
  private List<CApAPPWaypoint> m_synchronizedWaypointList;

  public CApAPPTask(ITxOperation i_operation) => this.m_operation = i_operation;

  public ITxOperation Operation => this.m_operation;

  public string Name => ((ITxObject) this.Operation).Name;

  public KiAppTaskEx KiTask
  {
    get => this.m_kiTask;
    set => this.m_kiTask = value;
  }

  public bool IsValid => this.m_kiTask.IsValid;

  public List<CApAPPWaypoint> SynchronizedWaypointList
  {
    get => this.m_synchronizedWaypointList;
    set
    {
      this.m_synchronizedWaypointList = new List<CApAPPWaypoint>();
      for (int index = 0; index < value.Count; ++index)
        this.m_synchronizedWaypointList.Add(new CApAPPWaypoint(value[index]));
    }
  }

  public int CountWaypoints() => this.m_kiTask.CountWaypoints();

  public virtual CApAPPWaypoint Waypoint(int i_index) => (CApAPPWaypoint) null;

  public virtual void Waypoint(int i_index, CApAPPWaypoint i_wp)
  {
    this.m_kiTask.SetWaypoint(i_index, i_wp.KiWaypoint);
  }

  public virtual List<CApAPPWaypoint> Waypoints()
  {
    List<CApAPPWaypoint> capAppWaypointList = new List<CApAPPWaypoint>();
    for (int i_index = 0; i_index < this.CountWaypoints(); ++i_index)
      capAppWaypointList.Add(this.Waypoint(i_index));
    return capAppWaypointList;
  }

  public void Waypoints(List<CApAPPWaypoint> i_waypoints)
  {
    List<KiAppWaypointEx> kiAppWaypointExList = new List<KiAppWaypointEx>();
    for (int index = 0; index < i_waypoints.Count; ++index)
      kiAppWaypointExList.Add(i_waypoints[index].KiWaypoint);
    this.KiTask.SetWaypoints(kiAppWaypointExList);
  }

  public virtual List<CApAPPWaypoint> CreateWaypointListCopy()
  {
    List<CApAPPWaypoint> capAppWaypointList = this.Waypoints();
    List<CApAPPWaypoint> waypointListCopy = new List<CApAPPWaypoint>();
    for (int index = 0; index < capAppWaypointList.Count; ++index)
      waypointListCopy.Add(new CApAPPWaypoint(capAppWaypointList[index]));
    return waypointListCopy;
  }

  protected void configureWaypoint(CApAPPWaypoint i_wp, int i_index)
  {
    i_wp.Task = this;
    i_wp.WaypointRank = i_index;
  }

  public virtual void DeactivateAllRanges() => this.m_kiTask.DeactivateAllRanges();

  public virtual void SetActiveRange(int i_firstIndex, int i_lastIndex)
  {
    this.m_kiTask.SetActiveRange(i_firstIndex, i_lastIndex);
  }

  public virtual void GetActiveRange(ref int o_firstIndex, ref int o_lastIndex)
  {
    o_firstIndex = -1;
    o_lastIndex = -1;
    for (int i_index = 0; i_index < this.CountWaypoints(); ++i_index)
    {
      if (this.Waypoint(i_index).Active)
      {
        o_firstIndex = i_index;
        break;
      }
    }
    for (int i_index = this.CountWaypoints() - 1; i_index >= 0; --i_index)
    {
      if (this.Waypoint(i_index).Active)
      {
        o_lastIndex = i_index;
        break;
      }
    }
  }

  public void UpdateSynchronizedWaypointList() => this.SynchronizedWaypointList = this.Waypoints();

  public void SetConsiderActiveSectionVolumes(bool i_considerActSectionVolume)
  {
    this.m_kiTask.ConsiderActiveSectionVolumes = i_considerActSectionVolume;
  }

  public void SetConsiderActiveSectionVolumesForDevice(bool i_considerActSectionVolume)
  {
    this.m_kiTask.ConsiderActiveSectionVolumesForDevice = i_considerActSectionVolume;
  }

  public bool GetConsiderActiveSectionVolumes() => this.m_kiTask.ConsiderActiveSectionVolumes;

  public bool GetConsiderActiveSectionVolumesForDevice()
  {
    return this.m_kiTask.ConsiderActiveSectionVolumesForDevice;
  }

  public void ClearTask()
  {
    if (this.m_kiTask == null)
      return;
    this.m_kiTask.ClearTask();
  }

  protected void UpdateStateFromDescriptor(KiAppCellPositioningDescriptorEx i_cellDescriptor)
  {
    for (int index = 0; index < i_cellDescriptor.DeviceJointValuesPairsCount; ++index)
    {
      TxPoseData currentPose = (i_cellDescriptor.GetDeviceJointValuesPairAt(index).Key as ITxDevice).CurrentPose;
      currentPose.JointValues = i_cellDescriptor.GetDeviceJointValuesPairAt(index).Value;
      (i_cellDescriptor.GetDeviceJointValuesPairAt(index).Key as ITxDevice).CurrentPose = currentPose;
    }
    for (int index = 0; index < i_cellDescriptor.ElemLocationPairsCount; ++index)
    {
      try
      {
        (i_cellDescriptor.GetElemLocationPairAt(index).Key as ITxLocatableObject).AbsoluteLocation = i_cellDescriptor.GetElemLocationPairAt(index).Value;
      }
      catch (TxScopeIsReadOnlyException ex)
      {
      }
      catch (TxAppearanceCanNotChangeChildException ex)
      {
      }
    }
    for (int index = 0; index < i_cellDescriptor.DeviceTCPFLocationPairsCount; ++index)
    {
      try
      {
        if (i_cellDescriptor.GetDeviceTCPFLocationPairAt(index).Key is ITxTool)
          (i_cellDescriptor.GetDeviceTCPFLocationPairAt(index).Key as ITxTool).TCPF.AbsoluteLocation = i_cellDescriptor.GetDeviceTCPFLocationPairAt(index).Value;
        else if (i_cellDescriptor.GetDeviceTCPFLocationPairAt(index).Key is ITxRobot)
          (i_cellDescriptor.GetDeviceTCPFLocationPairAt(index).Key as ITxRobot).TCPF.AbsoluteLocation = i_cellDescriptor.GetDeviceTCPFLocationPairAt(index).Value;
      }
      catch (TxScopeIsReadOnlyException ex)
      {
      }
      catch (TxAppearanceCanNotChangeChildException ex)
      {
      }
    }
  }

  public bool NeedsPostProcess(KiAppPathPlanningParamsEx planningParams)
  {
    return this.m_kiTask is KiAppRoboticTaskEx kiTask && kiTask.NeedsPostProcess(planningParams);
  }

  public virtual void RestoreInitialState()
  {
  }

  public virtual void UpdateExternalAxis()
  {
    TxApplicationEx.FireDocumentUpdateStarted();
    int o_firstIndex = -1;
    int o_lastIndex = -1;
    this.GetActiveRange(ref o_firstIndex, ref o_lastIndex);
    for (int index = o_firstIndex; index <= o_lastIndex; ++index)
    {
      if (this.Waypoint(index).Fixed)
      {
        ITxObject location = this.GetLocation(index);
        this.UpdateExternalAxis(this.Waypoint(index), location);
      }
    }
    TxApplicationEx.FireDocumentUpdateEnded();
  }

  public virtual void UpdateExternalAxis(CApAPPWaypoint i_waypoint, ITxObject i_location)
  {
  }

  public virtual void UpdateTxOperation(bool i_isUndo = false)
  {
    TxApplicationEx.FireDocumentUpdateStarted();
    this.RemoveUnstableIntermediateLocations(i_isUndo);
    int o_firstIndex = -1;
    int o_lastIndex = -1;
    this.GetActiveRange(ref o_firstIndex, ref o_lastIndex);
    if (o_firstIndex != -1 && o_lastIndex != -1)
    {
      ITxObject location = this.GetLocation(o_firstIndex);
      for (int index = o_firstIndex; index <= o_lastIndex; ++index)
      {
        if (index > o_firstIndex && index < o_lastIndex && !this.Waypoint(index).Fixed)
          this.AddLocationFromWaypoint(this.Waypoint(index), location);
        else
          this.UpdateLocationFromWaypoint(this.Waypoint(index), this.GetLocation(index));
        location = this.GetLocation(index);
      }
    }
    this.KiTask.UpdateTxOperationLocationIndexes();
    TxApplicationEx.FireDocumentUpdateEnded();
  }

  public void UpdateWaypointsFromSubTasks() => this.KiTask.UpdateWaypointsFromSubTasks();

  public abstract void AddLocationFromWaypoint(
    CApAPPWaypoint i_waypoint,
    ITxObject i_previousLocation);

  public virtual void UpdateLocationFromWaypoint(CApAPPWaypoint i_waypoint, ITxObject i_location)
  {
  }

  public virtual void UpdateApproachDepartTxOperation(
    List<CApAPPWaypoint> initialWaypoint,
    bool i_isUndo = false)
  {
  }

  public bool UpdatePlanSuccesses(List<CApAPPWaypointPlanSuccess> i_planSuccesses)
  {
    List<KiAppWaypointPlanSuccess> waypointPlanSuccessList = new List<KiAppWaypointPlanSuccess>();
    for (int index = 0; index < i_planSuccesses.Count; ++index)
      waypointPlanSuccessList.Add((KiAppWaypointPlanSuccess) i_planSuccesses[index]);
    return this.KiTask.UpdatePlanSuccesses(waypointPlanSuccessList);
  }

  public List<CApAPPWaypointPlanSuccess> GetPlanSuccesses()
  {
    List<CApAPPWaypointPlanSuccess> planSuccesses1 = new List<CApAPPWaypointPlanSuccess>();
    List<KiAppWaypointPlanSuccess> planSuccesses2 = this.KiTask.GetPlanSuccesses();
    for (int index = 0; index < planSuccesses2.Count; ++index)
      planSuccesses1.Add((CApAPPWaypointPlanSuccess) planSuccesses2[index]);
    return planSuccesses1;
  }

  public void ClearWaypointPlanSuccess()
  {
    for (int i_index = 0; i_index < this.CountWaypoints(); ++i_index)
      this.Waypoint(i_index).WaypointPlanSuccess = CApAPPWaypointPlanSuccess.Regular;
  }

  public void UpdateWaypointsStatus() => this.KiTask.UpdateWaypointsStatus();

  public void UpdateWaypointStatus(int index) => this.KiTask.UpdateWaypointStatus(index);

  public virtual bool RemoveUnstableIntermediateLocations(bool i_isUndo)
  {
    TxObjectList allLocations = this.GetAllLocations();
    int o_firstIndex = -1;
    int o_lastIndex = -1;
    this.GetActiveRange(ref o_firstIndex, ref o_lastIndex);
    if (o_firstIndex != -1 && o_lastIndex != -1)
    {
      int num = o_lastIndex + ((Collection<ITxObject>) allLocations).Count - this.CountWaypoints();
      for (int index = o_firstIndex + 1; index < num; ++index)
      {
        if (i_isUndo && !this.IsLockedLocation(((Collection<ITxObject>) allLocations)[index]) || !i_isUndo && !this.SynchronizedWaypointList[index].Fixed)
          ((Collection<ITxObject>) allLocations)[index].Delete();
      }
    }
    return true;
  }

  protected virtual bool IsLockedLocation(ITxObject i_location) => false;

  public ITxObject GetFirstLocation()
  {
    ITxObject firstLocation = (ITxObject) null;
    if (((Collection<ITxObject>) this.GetAllLocations()).Count > 0)
      firstLocation = ((Collection<ITxObject>) this.GetAllLocations())[0];
    return firstLocation;
  }

  public ITxObject GetLastLocation()
  {
    ITxObject lastLocation = (ITxObject) null;
    if (((Collection<ITxObject>) this.GetAllLocations()).Count > 0)
      lastLocation = ((Collection<ITxObject>) this.GetAllLocations())[((Collection<ITxObject>) this.GetAllLocations()).Count - 1];
    return lastLocation;
  }

  public ITxObject GetFollowingLocation(ITxObject i_location)
  {
    ITxObject followingLocation = (ITxObject) null;
    TxObjectList allLocations = this.GetAllLocations();
    for (int index = 0; index < ((Collection<ITxObject>) allLocations).Count - 1; ++index)
    {
      if (((Collection<ITxObject>) allLocations)[index] == i_location)
        return ((Collection<ITxObject>) allLocations)[index + 1];
    }
    return followingLocation;
  }

  public ITxObject GetSecondToLastLocation()
  {
    ITxObject secondToLastLocation = (ITxObject) null;
    if (((Collection<ITxObject>) this.GetAllLocations()).Count > 1)
      secondToLastLocation = ((Collection<ITxObject>) this.GetAllLocations())[((Collection<ITxObject>) this.GetAllLocations()).Count - 2];
    return secondToLastLocation;
  }

  public abstract TxObjectList GetAllLocations();

  public ITxObject GetLocation(int i_rank)
  {
    ITxObject location = (ITxObject) null;
    TxObjectList allLocations = this.GetAllLocations();
    if (((Collection<ITxObject>) allLocations).Count > i_rank)
      location = ((Collection<ITxObject>) allLocations)[i_rank];
    return location;
  }

  public static CApAPPTask RetrieveSubTaskParentTask(
    CApAPPSubTask i_subTask,
    List<CApAPPTask> i_tasks)
  {
    CApAPPTask capAppTask = (CApAPPTask) null;
    for (int index = 0; index < i_tasks.Count; ++index)
    {
      if (i_tasks[index].Operation == i_subTask.Operation)
      {
        capAppTask = i_tasks[index];
        break;
      }
    }
    return capAppTask;
  }

  public virtual CApAPPSubTask SubTask(int i_index)
  {
    return new CApAPPSubTask(this.KiTask.SubTask(i_index))
    {
      Task = this
    };
  }

  public virtual int CountSubTasks() => this.KiTask.CountSubTasks();

  public int CountSubTasksToPlan()
  {
    int plan = 0;
    for (int i_index = 0; i_index < this.CountSubTasks(); ++i_index)
    {
      if (this.SubTask(i_index).IsToPlan())
        ++plan;
    }
    return plan;
  }

  public CApAPPSubTask SubTaskToPlan(int i_index)
  {
    CApAPPSubTask plan = (CApAPPSubTask) null;
    if (i_index < this.CountSubTasksToPlan())
    {
      int i_index1 = 0;
      int num = 0;
      for (; i_index1 < this.CountSubTasks(); ++i_index1)
      {
        if (this.SubTask(i_index1).IsToPlan())
        {
          if (i_index == num)
          {
            plan = this.SubTask(i_index1);
            break;
          }
          ++num;
        }
      }
    }
    return plan;
  }

  public int RetrieveWaypointIndexFromSubTask(CApAPPSubTask i_subTask)
  {
    return this.KiTask.RetrieveWaypointIndexFromSubTask(i_subTask.KiSubTask);
  }

  public int RetrieveSubTaskIndexFromWaypoint(CApAPPWaypoint i_wp)
  {
    return this.KiTask.RetrieveSubTaskIndexFromWaypointIndex(i_wp.WaypointRank);
  }
}
